package p.ithorns.framework.feign.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import p.ithorns.framework.core.model.Result;

/**
 * FeignErrorAdvice
 * 当Feign调用结果为非200的响应码时就触发了Feign的异常解析，Feign的异常解析器会将其包装成FeignException
 *
 * @author Ht.L
 * @date 2024-03-27 16:00
 * @since 1.0.0
 */
@RestControllerAdvice
public class FeignErrorAdvice {

    private final static Logger log = LoggerFactory.getLogger(FeignErrorAdvice.class);

    /**
     * 自定义业务异常处理。
     *
     * @param e the e
     * @return ResultData
     */
    @ExceptionHandler(BizException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> exception(BizException e) {
        log.error("业务异常 ex={}", e.getMessage(), e);
        return Result.failed(e.getMessage());
    }

}